import { selectChildrenType, selectOptions } from '../options';
import { CreatedOptionsProps } from '@/views/basic/created/interface';

const clearHideTypeValue = (basicForm, arr) => {
  for (let key of arr) {
    basicForm.form[key] = '';
  }
};

const selectItem: CreatedOptionsProps[] = [
  {
    type: 'input',
    sort: 8000,
    label: 'change方法',
    prop: 'changeMethod',
    hide: false,
    attrs: {
      type: 'textarea',
      autosize: {
        minRows: 6,
        maxRows: 10
      }
    }
  },
  {
    type: 'select',
    label: '数据类型',
    sort: 9900,
    prop: 'childrenProps.type',
    hide: false,
    children: selectChildrenType,
    attrs: {
      clearable: true
    },
    changeMethod: (form, basicForm) => {
      console.log(basicForm, 'basicForm.form', form);

      const optionsFormItem = ['childrenProps.options'];
      const dictCodeFormItem = ['childrenProps.dictCode', 'childrenProps.label', 'childrenProps.value'];
      const apiParamsFormItem = ['childrenProps.api'];
      const type = basicForm.form['childrenProps.type'];
      const hideListAll = [...optionsFormItem, ...dictCodeFormItem, ...apiParamsFormItem];
      clearHideTypeValue(basicForm, hideListAll);
      if (type) {
        if (type === 'options') {
          const hideList = [...dictCodeFormItem, ...apiParamsFormItem];
          formChangeItemStatus(basicForm, hideList, true, 'hide');
          formChangeItemStatus(basicForm, optionsFormItem, false, 'hide');
          clearHideTypeValue(basicForm, hideList);
        } else if (type === 'dict') {
          const hideList = [...optionsFormItem, ...apiParamsFormItem];
          formChangeItemStatus(basicForm, hideList, true, 'hide');
          formChangeItemStatus(basicForm, dictCodeFormItem, false, 'hide');
          clearHideTypeValue(basicForm, hideList);
          Object.assign(basicForm.form, {
            'childrenProps.label': 'dicCode',
            'childrenProps.value': 'dicValue'
          });
        } else if (type === 'api') {
          const hideList = [...optionsFormItem, ...dictCodeFormItem];
          formChangeItemStatus(basicForm, hideList, true, 'hide');
          formChangeItemStatus(basicForm, apiParamsFormItem, false, 'hide');
          clearHideTypeValue(basicForm, hideList);
        }
      } else {
        formChangeItemStatus(basicForm, hideListAll, true, 'hide');
      }
    }
  },
  {
    type: 'input',
    label: '选择数据',
    sort: 9800,
    prop: 'childrenProps.options',
    hide: true,
    attrs: {
      type: 'textarea'
    },
    rules: [
      {
        required: true,
        message: '选择数据',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '字典编号',
    sort: 9700,
    prop: 'childrenProps.dictCode',
    hide: true,
    rules: [
      {
        required: true,
        message: '字典编号',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '标签映射字段',
    sort: 9600,
    prop: 'childrenProps.label',
    hide: true,
    rules: [
      {
        required: true,
        message: '标签映射字段',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '值映射字段',
    sort: 9500,
    prop: 'childrenProps.value',
    hide: true,
    rules: [
      {
        required: true,
        message: '值映射字段',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '值映射字段',
    sort: 9400,
    prop: 'childrenProps.value',
    hide: true,
    rules: [
      {
        required: true,
        message: '值映射字段',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: 'API请求',
    sort: 9800,
    prop: 'childrenProps.api',
    hide: true,
    attrs: {
      type: 'textarea',
      autosize: { minRows: 10, maxRows: 10 }
    },
    rules: [
      {
        required: true,
        message: '',
        trigger: 'blur'
      }
    ]
  },

  {
    type: 'input',
    sort: 8900,
    label: '远程搜索',
    prop: 'attrs._remoteMethod',
    hide: false,
    attrs: {
      type: 'textarea'
    },
    inputMethod: form => {
      // 输入框远程搜索
      const flag = !!form['attrs._remoteMethod'];
      Object.assign(form, {
        'attrs.remote': flag,
        'attrs.filterable': flag
      });
    }
  },
  {
    type: 'select',
    label: '加载Loading',
    sort: 8890,
    prop: 'attrs.loading',
    defaultValue: false,
    attrs: {
      clearable: true
    },
    hide: false,
    children: selectOptions
  },
  {
    type: 'select',
    label: '远程加载',
    sort: 8800,
    prop: 'attrs.remote',
    attrs: {
      clearable: true
    },
    hide: false,
    children: selectOptions
  },
  // clearable,multiple,filterable
  {
    type: 'select',
    label: '多选',
    sort: 8500,
    prop: 'attrs.multiple',
    attrs: {
      clearable: true
    },
    hide: false,
    children: selectOptions
  },
  {
    type: 'select',
    label: '可搜索',
    sort: 8600,
    prop: 'attrs.filterable',
    attrs: {
      clearable: true
    },
    hide: false,
    children: selectOptions
  }
];

export { selectItem };
